package com.falm.picturebackend.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.falm.picturebackend.entity.DO.UserDO;
import com.falm.picturebackend.entity.VO.UserVO;
import com.falm.picturebackend.mapper.UserDOMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Component
public class UserDODao {

    @Resource
    private UserDOMapper userDOMapper;

    public UserDO getUserByUserAccount(String userAccount) {
        if (userDOMapper == null) {
            throw new RuntimeException("UserDOMapper is null, please check the configuration");
        }
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDO::getUserAccount, userAccount);
        return userDOMapper.selectOne(queryWrapper);
    }

    public void registerUser(UserDO userDO) {
        if (userDOMapper == null) {
            throw new RuntimeException("UserDOMapper is null, please check the configuration");
        }
        userDOMapper.insert(userDO);
    }

    public void deleteUser(String userAccount) {
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDO::getUserAccount, userAccount);
        userDOMapper.delete(queryWrapper);
    }

    public void updateUser(UserDO userDO) {
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDO::getUserAccount, userDO.getUserAccount());
        userDOMapper.update(userDO, queryWrapper);
    }

    public List<UserDO> getUser(UserDO userDO, String sortField, String sortOrder) {
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery();
        
        if (userDO != null) {
            // 对字符串类型字段进行判断
            if (StringUtils.isNotBlank(userDO.getUserAccount())) {
                queryWrapper.eq(UserDO::getUserAccount, userDO.getUserAccount());
            }
            if (StringUtils.isNotBlank(userDO.getUserName())) {
                queryWrapper.eq(UserDO::getUserName, userDO.getUserName());
            }
            if (StringUtils.isNotBlank(userDO.getUserProfile())) {
                queryWrapper.eq(UserDO::getUserProfile, userDO.getUserProfile());
            }
            if (StringUtils.isNotBlank(userDO.getUserRole())) {
                queryWrapper.eq(UserDO::getUserRole, userDO.getUserRole());
            }
            if (StringUtils.isNotBlank(userDO.getVipCode())) {
                queryWrapper.eq(UserDO::getVipCode, userDO.getVipCode());
            }
            
            // 对Long类型字段进行判断
            if (userDO.getId() != null) {
                queryWrapper.eq(UserDO::getId, userDO.getId());
            }
            if (userDO.getVipNumber() != null) {
                queryWrapper.eq(UserDO::getVipNumber, userDO.getVipNumber());
            }
            
            // 对Date类型字段进行判断
            if (userDO.getEditTime() != null) {
                queryWrapper.eq(UserDO::getEditTime, userDO.getEditTime());
            }
            if (userDO.getCreateTime() != null) {
                queryWrapper.eq(UserDO::getCreateTime, userDO.getCreateTime());
            }
            if (userDO.getUpdateTime() != null) {
                queryWrapper.eq(UserDO::getUpdateTime, userDO.getUpdateTime());
            }
            if (userDO.getVipExpireTime() != null) {
                queryWrapper.eq(UserDO::getVipExpireTime, userDO.getVipExpireTime());
            }
            if (sortField != null && sortOrder != null) {
                // 根据sortField字符串动态构建SFunction
                switch (sortField) {
                    case "id":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getId);
                        break;
                    case "userAccount":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getUserAccount);
                        break;
                    case "userName":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getUserName);
                        break;
                    case "userProfile":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getUserProfile);
                        break;
                    case "userRole":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getUserRole);
                        break;
                    case "vipCode":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getVipCode);
                        break;
                    case "vipNumber":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getVipNumber);
                        break;
                    case "editTime":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getEditTime);
                        break;
                    case "createTime":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getCreateTime);
                        break;
                    case "updateTime":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getUpdateTime);
                        break;
                    case "vipExpireTime":
                        queryWrapper.orderBy(true, sortOrder.equals("asc"), UserDO::getVipExpireTime);
                        break;
                    default:
                        // 如果没有匹配的字段，不进行排序
                        break;
                }
            }
        }
        
        return userDOMapper.selectList(queryWrapper);
    }

}